from typing import Callable, Any
from collections import defaultdict

class EventBus:
    def __init__(self):
        self._subscribers: dict[str, list[Callable[..., Any]]] = defaultdict(list)
    
    def subscribe(self, event_type: str, callback: Callable[..., Any]) -> None:
        """订阅事件"""
        self._subscribers[event_type].append(callback)
    
    def unsubscribe(self, event_type: str, callback: Callable[..., Any]) -> None:
        """取消订阅"""
        if event_type in self._subscribers:
            try:
                self._subscribers[event_type].remove(callback)
            except ValueError:
                pass
    
    def publish(self, event_type: str, *args: Any, **kwargs: Any) -> None:
        """发布事件"""
        for callback in self._subscribers.get(event_type, []):
            callback(*args, **kwargs)


# 使用示例
if __name__ == "__main__":
    bus = EventBus()
    
    # 定义订阅者
    def on_message_received(message: str) -> None:
        print(f"收到消息: {message}")
    
    def on_error_occurred(error: str) -> None:
        print(f"错误发生: {error}")
    
    # 订阅事件
    bus.subscribe("message", on_message_received)
    bus.subscribe("error", on_error_occurred)
    
    # 发布事件
    bus.publish("message", "Hello, Pub-Sub!")
    bus.publish("error", "Something went wrong")
    
    # 取消订阅
    bus.unsubscribe("message", on_message_received)
    bus.publish("message", "这条消息不会被接收")